#include "util.hpp"
#include "Log.hpp"
#include <pthread.h>

class Task{
public:
  Task(int sockfd, uint16_t port, std::string ip):_sockfd(sockfd),_port(port),_ip(ip)
  {}
  ~Task()
  {}
public:
  std::string run()
  {
    char buf[1024];
    std::string ret;
    while(true)
    {
      ssize_t s = read(_sockfd, &buf, sizeof(int));
      if(s < 0)
      {
        logMessage(FATAL,"read fail");
        exit(1);
      }
      else 
      {
        logMessage(DEBUG,"线程[%d]开始处理",pthread_self());
        //ret = to_binary(buf);
        logMessage(DEBUG,"处理完成");
      }
      write(_sockfd, (char *)ret.c_str(), ret.size());
    }

    close(_sockfd);
    return ret;
  }
  std::string to_binary(char *buf)
  {
    int i = atoi(buf);
    std::string ret;
    while(i)
    {
      ret += std::to_string(i%2);
      i /= 2;
      if(i == 1) i = 0;
    }
    //logMessage(DEBUG,"task ready");
    return ret;
  }
private:
  int _sockfd;
  uint16_t _port;
  std::string _ip;
};
